package com.apac.dao;

import java.util.List;

import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Root;

import com.apac.bom.AbstractBOM;
import com.apac.bom.AccountRequirement;
import com.apac.bom.Requirement;

public class RequirementDAO extends AbstractDAO {

	@Override
	public AbstractBOM findByKey(String id) {
		// TODO Auto-generated method stub
		int idx = 0;
		try {
			idx = Integer.valueOf(id);
		} catch (NumberFormatException e) {

		}
		return this.findByKey(idx);
	}

	public Requirement findByKey(int id) {
		Requirement requirement = null;

		requirement = em.find(Requirement.class, id);
		return requirement;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<AbstractBOM> findAll() {
		// TODO Auto-generated method stub
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Requirement> cq = cb.createQuery(Requirement.class);
		Root<Requirement> root = cq.from(Requirement.class);
		cq.select(root);

		TypedQuery tq = em.createQuery(cq);
		List<AbstractBOM> requirements = tq.getResultList();
		return requirements;
	}

	public List<AccountRequirement> findByAccount(String account) {
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<AccountRequirement> cq = cb
				.createQuery(AccountRequirement.class);
		Root<AccountRequirement> root = cq.from(AccountRequirement.class);
		ParameterExpression<String> nameParameter = cb.parameter(String.class,
				"acctname");
		cq.select(root).where(
				cb.equal(root.get("account").<String> get("acctName"),
						nameParameter));
		List<AccountRequirement> requirements = null;

		TypedQuery<AccountRequirement> tq = em.createQuery(cq);
		tq.setParameter("acctname", account);
		// TypedQuery<AccountRequirement> tq = em
		// .createQuery(
		// "select a from AccountRequirement a where a.account.acctName = :acctname",
		// AccountRequirement.class);
		// tq.setParameter("acctname", account);

		requirements = tq.getResultList();

		return requirements;
	}

	@Override
	public boolean update(AbstractBOM entity) {
		// TODO Auto-generated method stub
		try {
			Requirement requirement = this.findByKey(((Requirement) entity)
					.getRqmtCode());
			tx.begin();
//			requirement.setRqmtDesc(((Requirement) entity).getRqmtDesc());
//			requirement.setAccountRequirements(((Requirement) entity)
//					.getAccountRequirements());
			em.merge((Requirement)entity);
			em.flush();
			em.clear();
			tx.commit();

			return true;
		} catch (Exception e) {
			tx.rollback();
		} finally {
			em.close();
		}
		return false;
	}

	@Override
	public boolean delete(AbstractBOM entity) {
		// TODO Auto-generated method stub
		try {
			Requirement requirement = this.findByKey(((Requirement) entity)
					.getRqmtCode());
			tx.begin();
			em.remove(requirement);
			em.flush();
			em.clear();
			tx.commit();

			return true;
		} catch (Exception e) {
			tx.rollback();
		} finally {
			em.close();
		}
		return false;
	}
}
